<?php
require 'DB/db_data_loader.php';

/* Parameters */
switch (Addresing::get('load')) {
    case 'is':
        $load = 'is';
        break;
    default:
        $load = 'subdivision';
}
switch (Addresing::get('sort_by')) {
    case 'time':
        $sort_by = 'time';
        break;
    default:
        $sort_by = 'quantity';
}

$tab = Addresing::getRequestedTab();

/* Left menu */
if($load == 'is') $class1='class="current"';
else $class1='';
if($load != 'is') $class2='class="current"';
else $class2='';

ob_start();
Design::leftSubmenuBeginning("Apkrova");
$sidebar_content .= ob_get_clean();
$sidebar_content .= '
<ul>
    <li><a '.$class1.' href="?tab='.$tab.'&amp;load=is&amp;sort_by='.$sort_by.'">Informacinių sistemų</a></li>
    <li><a '.$class2.' href="?tab='.$tab.'&amp;load=subdivision&amp;sort_by='.$sort_by.'">Padalinių</a></li>
</ul>';
ob_start();
Design::leftSubmenuEnding();
$sidebar_content .= ob_get_clean();



if($sort_by == 'time') $class1='class="current"';
else $class1='';
if($sort_by != 'time') $class2='class="current"';
else $class2='';

ob_start();
Design::leftSubmenuBeginning("Rušiuoti pagal");
$sidebar_content .= ob_get_clean();
$sidebar_content .= '
<ul>
    <li><a '.$class1.' href="?tab='.$tab.'&amp;load='.$load.'&amp;sort_by=time">Sugaištą laiką</a></li>
    <li><a '.$class2.' href="?tab='.$tab.'&amp;load='.$load.'&amp;sort_by=quantity">Suadministruotą kiekį</a></li>
</ul>';
ob_start();
Design::leftSubmenuEnding();
$sidebar_content .= ob_get_clean();

/* Content */
Design::contentBeginning();


require 'Processing/subdivision_data_processing.php';
$dataset = SubdivisionData::counts_to_dataset();
$subdivisions = get_subdivisios_associative();
$measures = get_support_measures_associative();
$dates = SubdivisionData::get_dates_predicted(12);  
$time_groups = SubdivisionData::dataset_to_table($dataset, $subdivisions, $dates, $is_time_groups);
require 'SubdivisionsDesign.php';

/* Subdivisions */
if ($load == 'subdivision') {
    if (count($time_groups) > 0) {
        SubdivisionData::predict($time_groups);
        if ($sort_by == 'time') {
            Design::h1('Padalinių apkrova pagal sugaištą laiką');
            SubdivisionsDesign::table_time($dates, $subdivisions, $time_groups);
            echo "Laukelyje rodoma, kiek laiko padalinys paraiškas administravo.";
        } else {
            Design::h1('Padalinių apkrova pagal paraiškų skaičių');
            SubdivisionsDesign::table_quantity($dates, $subdivisions, $time_groups);
            echo "Laukelyje rodoma: gauta per mėnesį, likę nuo anskčiau, suadministruota.";
        }
    } else {
        echo "Duomenų apie istorinius kiekius nėra. Pirma importuokite juos.";
    }
      
/* Information system */
} else { 
    $is_names = get_is_associative();
    SubdivisionData::predict_is($is_time_groups);
    if ($sort_by == 'time') {
        Design::h1('Informacinių sistemų apkrova pagal sugaištą laiką');
        SubdivisionsDesign::is_quantity($dates, $is_names, $is_time_groups, true);
    } else {
        Design::h1('Informacinių sistemų apkrova pagal paraiškų skaičių');
        SubdivisionsDesign::is_quantity($dates, $is_names, $is_time_groups);
    }
    
    /**
     * @deprecated need better performance
     */
    if (false) {
        require 'Processing/is_data_processing.php';
        Design::contentEnding();

        if ($sort_by == 'time') {
            Design::h1('Informacinių sistemų apkrova pagal sugaištą laiką');
            ?>
            </pre>
            <table border="3"><tr><th>IS/DATE</th><th>2010-01</th>
                <th>2010-02</th><th>2010-03</th><th>2010-04</th><th>2010-05</th>
                <th>2010-06</th><th>2010-07</th><th>2010-08</th><th>2010-09</th>
                <th>2010-10</th><th>2010-11</th><th>2010-12</th>
                <th>TOTAL</th></tr>
                <?
                $all_is = get_information_systems();

                foreach ($all_is["CODE"] as $row => $is_code) {
                    ?>
                    <tr><td><? echo $is_code; ?></td>
                <td><? echo get_is_work_by_month($is_code, "2010-01"); ?></td>
                <td><? echo get_is_work_by_month($is_code, "2010-02"); ?></td>
                <td><? echo get_is_work_by_month($is_code, "2010-03"); ?></td>
                <td><? echo get_is_work_by_month($is_code, "2010-04"); ?></td>
                <td><? echo get_is_work_by_month($is_code, "2010-05"); ?></td>
                <td><? echo get_is_work_by_month($is_code, "2010-06"); ?></td>
                <td><? echo get_is_work_by_month($is_code, "2010-07"); ?></td>
                <td><? echo get_is_work_by_month($is_code, "2010-08"); ?></td>
                <td><? echo get_is_work_by_month($is_code, "2010-09"); ?></td>
                <td><? echo get_is_work_by_month($is_code, "2010-10"); ?></td>
                <td><? echo intval(get_planned_is_work_by_month($is_code, "01")); ?></td>
                <td><? echo intval(get_planned_is_work_by_month($is_code, "12")); ?></td>
                <td><? echo get_is_work($is_code) ?></td>
                </tr> <?
                }
                ?>            
            </table>
        <?
        } else {
            Design::h1('Informacinių sistemų apkrova pagal paraiškų skaičių');?>
            </pre>
            <table border="3"><tr><th>IS/DATE</th><th>2010-01</th>
                <th>2010-02</th><th>2010-03</th><th>2010-04</th><th>2010-05</th>
                <th>2010-06</th><th>2010-07</th><th>2010-08</th><th>2010-09</th>
            <th>2010-10</th><th>2011-01</th><th>2011-02</th>
                <th>TOTAL</th></tr>
                <?
                $all_is = get_information_systems();

                foreach ($all_is["CODE"] as $row => $is_code) {
                    ?>
                    <tr><td><? echo $is_code; ?></td>
                <td><? echo get_is_application_count_by_month($is_code, "2010-01"); ?></td>
                <td><? echo get_is_application_count_by_month($is_code, "2010-02"); ?></td>
                <td><? echo get_is_application_count_by_month($is_code, "2010-03"); ?></td>
                <td><? echo get_is_application_count_by_month($is_code, "2010-04"); ?></td>
                <td><? echo get_is_application_count_by_month($is_code, "2010-05"); ?></td>
                <td><? echo get_is_application_count_by_month($is_code, "2010-06"); ?></td>
                <td><? echo get_is_application_count_by_month($is_code, "2010-07"); ?></td>
                <td><? echo get_is_application_count_by_month($is_code, "2010-08"); ?></td>
                <td><? echo get_is_application_count_by_month($is_code, "2010-09"); ?></td>
                <td><? echo get_is_application_count_by_month($is_code, "2010-10"); ?></td>
                <td><? echo intval(get_planned_is_application_count_by_month($is_code, "01")); ?></td>
                <td><? echo intval(get_planned_is_application_count_by_month($is_code, "02")); ?></td>
                <td><? echo get_is_application_count($is_code) ?></td>
                </tr> <?
                }
                ?>            
            </table>
        <? 
        }
    }
}
?>
<br/>
Tamsesniame fone rodoma prognozuojama apkrova.<br/>
<!--Norėdami matyti visą lentelę, du kartus ant jos bakstelėkite pele.-->

<?php
Design::contentEnding();
